package df.bean.process;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import df.bean.db.conn.DBConn;
import df.bean.obj.util.JDate;

public class ProcessPartialPayment {
	private DBConn dbConn;
	private String sql;
	private String hospitalCode;
	private ArrayList<HashMap<String, String>> arrData = new ArrayList<HashMap<String,String>>();

	public boolean processPartial(String hospitalCode, String mm, String yyyy, String startDate, String endDate){
		boolean result = true;
		this.hospitalCode = hospitalCode;
		this.arrData = this.getLineNo(startDate, endDate);
		String isPartial = "";
		try {
			this.dbConn = new DBConn(true);
			this.dbConn.setStatement();
			isPartial = this.dbConn.getSingleData("SELECT IS_PARTIAL FROM HOSPITAL WHERE CODE = '"+this.hospitalCode+"'");
			this.dbConn.getStatement().close();
			this.dbConn.closeDB("Close DB from "+this.getClass());
		} catch (SQLException e) {
			System.out.println("Cannot get is partial condition from hospital : "+e);
		}
		if(isPartial.equals("Y")){
			System.out.println("\nPartial Payment Processing");
			if(this.backUpData(yyyy,mm)){
				System.out.println("Start Partial Process : "+JDate.getTime());
				result = this.culculatePartial(this.arrData,mm,yyyy);
				System.out.println("Finish Partial Process : "+JDate.getTime());
			}else{
				result = false;
			}
		}
		return result;
	}	
	private ArrayList<HashMap<String, String>> getLineNo(String startDate, String endDate){
		this.dbConn = new DBConn();
		ArrayList<HashMap<String, String>> arrData = new ArrayList<HashMap<String, String>>();
		this.sql = "SELECT DISTINCT T.LINE_NO, T.OLD_DR_AMT, I.RECEIPT_DATE, I.RECEIPT_NO, I.TRANSACTION_DATE, T.INVOICE_NO, T.INVOICE_DATE "
		+ "FROM TRN_DAILY T ,INT_ERP_AR_RECEIPT I "//,INT_ERP_AR_RECEIPT I
		+ "WHERE T.HOSPITAL_CODE ='"+this.hospitalCode+"' AND T.INVOICE_NO = I.BILL_NO "// AND T.DR_AMT != 0 AND T.INVOICE_NO = I.BIL_NO
		+ "AND T.HOSPITAL_CODE = I.HOSPITAL_CODE "
		+ "AND (I.TRANSACTION_DATE BETWEEN '"+startDate+"' AND '"+endDate+"') "
		+ "AND I.DOC_TYPE= 'R' AND I.IS_LAST_RECEIPT ='N' AND T.IS_PARTIAL='N' AND T.BATCH_NO = '' AND T.YYYY = '' "
		+ "ORDER BY T.INVOICE_NO, T.LINE_NO";
		try {
			this.dbConn.setStatement();
			arrData = this.dbConn.getMultiData(this.sql);
			this.dbConn.closeDB("");
		} catch (SQLException e) {
			System.out.println("Partial Payment Process Error on statement : "+this.sql);
		}
		return arrData;
	}
	private boolean culculatePartial(ArrayList<HashMap<String, String>> arrLineNo ,String mm, String yyyy){
		boolean result = true; 
		String trnDate = "";
		this.dbConn = new DBConn(false);
		try { this.dbConn.setStatement(); } catch (SQLException e1) {}
		HashMap<String, String > hashData = new HashMap<String, String>();
		for(int i=0;i<arrLineNo.size();i++){
			hashData = arrLineNo.get(i);
				this.sql = "INSERT INTO TRN_DAILY (" +
				" HOSPITAL_CODE," +
				" INVOICE_NO," +
				" INVOICE_DATE," +
				" RECEIPT_NO," +
				" RECEIPT_DATE," +
				" TRANSACTION_DATE," +
				" HN_NO," +
				" PATIENT_NAME," +
				" EPISODE_NO," +
				" NATIONALITY_CODE," +
				" NATIONALITY_DESCRIPTION," +
				" PAYOR_OFFICE_CODE," +
				" PAYOR_OFFICE_NAME, " +
				" TRANSACTION_MODULE," +
				" TRANSACTION_TYPE," +
				" PAYOR_OFFICE_CATEGORY_CODE," +
				" PAYOR_OFFICE_CATEGORY_DESCRIPTION," +
				" IS_WRITE_OFF," +
				" LINE_NO," +
				" ADMISSION_TYPE_CODE,"+ 
				" PATIENT_DEPARTMENT_CODE," +
				" PATIENT_LOCATION_CODE," +
				" RECEIPT_DEPARTMENT_CODE," +
				" RECEIPT_LOCATION_CODE," +
				" DOCTOR_DEPARTMENT_CODE," +
				" ORDER_ITEM_CODE," +
				" ORDER_ITEM_DESCRIPTION," +
				" DOCTOR_CODE," +
				" VERIFY_DATE,"+
				" VERIFY_TIME," +
				" DOCTOR_EXECUTE_CODE," +
				" EXECUTE_DATE," +
				" EXECUTE_TIME," +
				" DOCTOR_RESULT_CODE," +
				" OLD_DOCTOR_CODE," +
				" RECEIPT_TYPE_CODE," +
				" AMOUNT_BEF_DISCOUNT," +
				" AMOUNT_OF_DISCOUNT," +
				" AMOUNT_AFT_DISCOUNT," +
				" AMOUNT_BEF_WRITE_OFF," +
				" INV_IS_VOID," +
				" REC_IS_VOID," +
				" UPDATE_DATE," +
				" UPDATE_TIME," +
				" USER_ID," +
				" BATCH_NO," +
				" YYYY," +
				" MM," +
				" DD," +
				" NOR_ALLOCATE_AMT," +
				" NOR_ALLOCATE_PCT," +
				" DR_AMT," +
				" OLD_DR_AMT," +
				" DR_TAX_400," +
				" DR_TAX_401," +
				" DR_TAX_402," +
				" DR_TAX_406," +
				" TAX_TYPE_CODE," +
				" DR_PREMIUM," +
				" GUARANTEE_PAID_AMT," +
				" GUARANTEE_AMT," +
				" GUARANTEE_CODE," +
				" GUARANTEE_DR_CODE," +
				" GUARANTEE_TYPE," +
				" GUARANTEE_DATE_TIME," +
				" GUARANTEE_TERM_MM," +
				" GUARANTEE_TERM_YYYY," +
				" GUARANTEE_NOTE," +
				" IS_GUARANTEE," +
				" HP_AMT," +
				" HP_PREMIUM," +
				" HP_TAX," +
				" COMPUTE_DAILY_DATE," +
				" COMPUTE_DAILY_TIME," +
				" COMPUTE_DAILY_USER_ID," +
				" DOCTOR_CATEGORY_CODE," +
				" EXCLUDE_TREATMENT," +
				" PREMIUM_CHARGE_PCT," +
				" PREMIUM_REC_AMT," +
				" ACTIVE," +
				" INVOICE_TYPE," +
				" TOTAL_BILL_AMOUNT," +
				" TOTAL_DR_REC_AMOUNT," +
				" OLD_AMOUNT," +
				" PAY_BY_CASH," +
				" PAY_BY_AR," +
				" PAY_BY_DOCTOR," +
				" PAY_BY_PAYOR," +
				" PAY_BY_CASH_AR," +
				" IS_PAID," +
				" ORDER_ITEM_ACTIVE," +
				" ORDER_ITEM_CATEGORY_CODE," +
				" WRITE_OFF_BILL_AMT," +
				" WRITE_OFF_RECEIPT_AMT," +
				" OLD_DR_AMT_BEF_WRITE_OFF," +
				" DR_AMT_BEF_WRITE_OFF," +
				" DR_PREMIUM_BEF_WRITE_OFF," +
				" HP_AMT_BEF_WRITE_OFF," +
				" HP_PREMIUM_WRITE_OFF," +
				" OLD_TAX_AMT," +
				" DR_TAX_406_BEF_WRITE_OFF," +
				" TAX_FROM_ALLOCATE," +
				" IS_GUARANTEE_FROM_ALLOC," +
				" IS_PARTIAL) " +
			"SELECT " +
				" HOSPITAL_CODE," +
				" INVOICE_NO," +
				" INVOICE_DATE," +
				" '"+hashData.get("RECEIPT_NO")+"'," + 
				" '"+hashData.get("RECEIPT_DATE")+"'," +
				//NEW EDIT TRANSACTION_DATE FOR PARTIAL PAYMENT
				" CASE WHEN '"+hashData.get("TRANSACTION_DATE")+"' = INVOICE_DATE THEN"+
				" CASE WHEN '"+hashData.get("TRANSACTION_DATE")+"' <= SUBSTRING('"+hashData.get("TRANSACTION_DATE")+"',1,6)+'15'"+
				" THEN SUBSTRING('"+hashData.get("TRANSACTION_DATE")+"',1,6)+'16'"+
				" ELSE SUBSTRING('"+hashData.get("TRANSACTION_DATE")+"',1,6)+'14' END"+
				" ELSE '"+hashData.get("TRANSACTION_DATE")+"' END AS TRANSACTION_DATE,"+
				//END NEW EDIT TRANSACTION_DATE FOR PARTIAL PAYEMENT
				//" '"+hashData.get("TRANSACTION_DATE")+"'," + //TRANSACTION_DATE
				" HN_NO," + 
				" PATIENT_NAME," +
				" EPISODE_NO,"+ 
				" NATIONALITY_CODE," + 
				" NATIONALITY_DESCRIPTION," + 
				" PAYOR_OFFICE_CODE," + 
				" PAYOR_OFFICE_NAME," + 
				" 'AR'," + 
				" TRANSACTION_TYPE," + 
				" PAYOR_OFFICE_CATEGORY_CODE," +
				" PAYOR_OFFICE_CATEGORY_DESCRIPTION," +
				" IS_WRITE_OFF," + 
				" '"+hashData.get("LINE_NO")+"'," + 
				" ADMISSION_TYPE_CODE,"+ 
				" PATIENT_DEPARTMENT_CODE," +
				" PATIENT_LOCATION_CODE," + 
				" RECEIPT_DEPARTMENT_CODE," +
				" RECEIPT_LOCATION_CODE," + 
				" DOCTOR_DEPARTMENT_CODE," +
				" ORDER_ITEM_CODE," + 
				" ORDER_ITEM_DESCRIPTION," + 
				" DOCTOR_CODE," + 
				" VERIFY_DATE," + 
				" VERIFY_TIME," + 
				" DOCTOR_EXECUTE_CODE," + 
				" EXECUTE_DATE," + 
				" EXECUTE_TIME," + 
				" DOCTOR_RESULT_CODE," +
				" OLD_DOCTOR_CODE," + 
				" RECEIPT_TYPE_CODE," + 
				" AMOUNT_BEF_DISCOUNT," +
				" AMOUNT_OF_DISCOUNT," +
				//----AMOUNT_AFT_DISCOUNT
				" CASE WHEN (SELECT ((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)*T.OLD_DR_AMT)/100)" +
				" FROM TRN_DAILY T, INT_ERP_AR_RECEIPT I " + 
				" WHERE T.INVOICE_NO = I.BILL_NO AND T.HOSPITAL_CODE = I.HOSPITAL_CODE" +
				" AND T.LINE_NO = '"+hashData.get("LINE_NO")+"' AND I.IS_LOADED = 'N'" +
				" AND I.RECEIPT_DATE = '"+hashData.get("RECEIPT_DATE")+"'" +
				" AND I.RECEIPT_NO = '"+hashData.get("RECEIPT_NO")+"' AND T.IS_PARTIAL ='N') > DR_AMT " +
				" THEN AMOUNT_AFT_DISCOUNT" + //IF Partial allocate > amount for payment then use that amount
				" ELSE " +
				" (SELECT ((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)* (T.AMOUNT_BEF_DISCOUNT - T.AMOUNT_OF_DISCOUNT))/100)" +
				" FROM TRN_DAILY T,INT_ERP_AR_RECEIPT I"+
				" WHERE T.INVOICE_NO = I.BILL_NO AND T.HOSPITAL_CODE = I.HOSPITAL_CODE" +
				" AND T.LINE_NO = '"+hashData.get("LINE_NO")+"' AND I.IS_LOADED = 'N'" +
				" AND I.RECEIPT_DATE = '"+hashData.get("RECEIPT_DATE")+"'" +
				" AND I.RECEIPT_NO = '"+hashData.get("RECEIPT_NO")+"' AND T.IS_PARTIAL ='N') END, "+
				//----AMOUNT_AFT_DISCOUNT
				" AMOUNT_BEF_WRITE_OFF," +
				" INV_IS_VOID," +
				" REC_IS_VOID," +
				" UPDATE_DATE," +
				" UPDATE_TIME," +
				" USER_ID," +
				" BATCH_NO," +
				" '"+yyyy+"'," +
				" '"+mm+"'," +
				" DD," +
				" NOR_ALLOCATE_AMT," +
				" NOR_ALLOCATE_PCT," +
				//----DR_AMT
				" CASE WHEN (SELECT ((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)*T.OLD_DR_AMT)/100)" +
				" FROM TRN_DAILY T, INT_ERP_AR_RECEIPT I " + 
				" WHERE T.INVOICE_NO = I.BILL_NO AND T.HOSPITAL_CODE = I.HOSPITAL_CODE" +
				" AND T.LINE_NO = '"+hashData.get("LINE_NO")+"' AND I.IS_LOADED = 'N'" +
				" AND I.RECEIPT_DATE = '"+hashData.get("RECEIPT_DATE")+"'" +
				" AND I.RECEIPT_NO = '"+hashData.get("RECEIPT_NO")+"' AND T.IS_PARTIAL ='N') > DR_AMT " +
				" THEN DR_AMT" + //IF Partial allocate > amount for payment then use that amount
				" ELSE (SELECT ((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)*T.OLD_DR_AMT)/100)" +
				" FROM TRN_DAILY T,INT_ERP_AR_RECEIPT I " + 
				" WHERE T.INVOICE_NO = I.BILL_NO AND T.HOSPITAL_CODE = I.HOSPITAL_CODE" +
				" AND T.LINE_NO = '"+hashData.get("LINE_NO")+"' AND I.IS_LOADED = 'N'" +
				" AND I.RECEIPT_DATE = '"+hashData.get("RECEIPT_DATE")+"'" +
				" AND I.RECEIPT_NO = '"+hashData.get("RECEIPT_NO")+"' AND T.IS_PARTIAL ='N') END, "+
				//----DR_AMT
				" OLD_DR_AMT," +
				" DR_TAX_400," +
				" DR_TAX_401," +
				" DR_TAX_402," +
				//----DR_TAX_406
				" CASE WHEN (SELECT ((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)*T.OLD_DR_AMT)/100)" +
				" FROM TRN_DAILY T, INT_ERP_AR_RECEIPT I " + 
				" WHERE T.INVOICE_NO = I.BILL_NO AND T.HOSPITAL_CODE = I.HOSPITAL_CODE" +
				" AND T.LINE_NO = '"+hashData.get("LINE_NO")+"' AND I.IS_LOADED = 'N'" +
				" AND I.RECEIPT_DATE = '"+hashData.get("RECEIPT_DATE")+"'" +
				" AND I.RECEIPT_NO = '"+hashData.get("RECEIPT_NO")+"' AND T.IS_PARTIAL ='N') > DR_AMT " +
				" THEN DR_TAX_406" + //IF Partial allocate > amount for payment then use that amount
				" ELSE (SELECT ((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)*T.DR_TAX_BEF_PARTIAL)/100)" +
				" FROM TRN_DAILY T, INT_ERP_AR_RECEIPT I"+ 
				" WHERE T.INVOICE_NO = I.BILL_NO AND T.HOSPITAL_CODE = I.HOSPITAL_CODE" +
				" AND T.LINE_NO = '"+hashData.get("LINE_NO")+"' AND I.IS_LOADED = 'N'" +
				" AND I.RECEIPT_DATE = '"+hashData.get("RECEIPT_DATE")+"'" +
				" AND I.RECEIPT_NO = '"+hashData.get("RECEIPT_NO")+"' AND T.IS_PARTIAL ='N') END,"+ 
				//----DR_TAX_406
				" TAX_TYPE_CODE," +
				" DR_PREMIUM," +
				" GUARANTEE_PAID_AMT," +
				" GUARANTEE_AMT," +
				" GUARANTEE_CODE," +
				" GUARANTEE_DR_CODE," +
				" GUARANTEE_TYPE," +
				" GUARANTEE_DATE_TIME," +
				" GUARANTEE_TERM_MM," +
				" GUARANTEE_TERM_YYYY," +
				" GUARANTEE_NOTE," +
				" IS_GUARANTEE," +
				" '0', "+
				//" HP_AMT," +
				" HP_PREMIUM," +
				" HP_TAX," +
				" COMPUTE_DAILY_DATE," +
				" COMPUTE_DAILY_TIME," +
				" COMPUTE_DAILY_USER_ID," +
				" DOCTOR_CATEGORY_CODE," +
				" EXCLUDE_TREATMENT," +
				" PREMIUM_CHARGE_PCT," +
				" PREMIUM_REC_AMT," +
				" ACTIVE," +
				" INVOICE_TYPE," +
				" TOTAL_BILL_AMOUNT," +
				" TOTAL_DR_REC_AMOUNT," +
				" OLD_AMOUNT," +
				" PAY_BY_CASH," +
				" 'Y'," +
				" PAY_BY_DOCTOR," +
				" PAY_BY_PAYOR," +
				" PAY_BY_CASH_AR," +
				" IS_PAID," +
				" ORDER_ITEM_ACTIVE," +
				" ORDER_ITEM_CATEGORY_CODE," +
				" WRITE_OFF_BILL_AMT," +
				" WRITE_OFF_RECEIPT_AMT," +
				" OLD_DR_AMT_BEF_WRITE_OFF," +
				" DR_AMT_BEF_WRITE_OFF," +
				" DR_PREMIUM_BEF_WRITE_OFF," +
				" HP_AMT_BEF_WRITE_OFF," +
				" HP_PREMIUM_WRITE_OFF," +
				" OLD_TAX_AMT," +
				" DR_TAX_406_BEF_WRITE_OFF," +
				" TAX_FROM_ALLOCATE," +
				" IS_GUARANTEE_FROM_ALLOC," +
				" 'Y'" +
				" FROM TRN_DAILY "+ 
				" WHERE LINE_NO = '"+hashData.get("LINE_NO")+"' AND IS_PARTIAL = 'N' AND DR_AMT > 0 AND HOSPITAL_CODE = '"+this.hospitalCode+"'";
				//System.out.println("sql = " + this.sql);
				try {
					this.dbConn.insert(this.sql);
				} catch (SQLException e) {
					System.out.println("error partial on statement : "+this.sql);
					//this.dbConn.rollDB();
					result = false;
					System.out.println("SQLException in method \"culculatePratial\" By LineNo =\""+hashData.get("LINE_NO")+"\" Error => "+e.getMessage());
				}
				if(result){
					this.sql = "UPDATE TRN_DAILY SET "
					+ "DR_AMT = CASE WHEN ((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)*T.OLD_DR_AMT)/100) > T.DR_AMT "
					+ "THEN 0 "
					+ "ELSE T.DR_AMT_BEF_PARTIAL-((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT) * T.OLD_DR_AMT)/100) END, "
					+ "DR_TAX_406 = CASE WHEN ((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)*T.OLD_DR_AMT)/100) > T.DR_AMT "
					+ "THEN 0 "
					+ "ELSE T.DR_TAX_BEF_PARTIAL-((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)*T.OLD_TAX_AMT)/100) END, "
					+ "AMOUNT_AFT_DISCOUNT = CASE WHEN ((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)*T.OLD_DR_AMT)/100) > T.DR_AMT "
					+ "THEN 0 "
					+ "ELSE T.AMOUNT_BEF_WRITE_OFF-((((100*I.PAYMENT_AMOUNT)/I.BILL_AMOUNT)* (T.AMOUNT_BEF_DISCOUNT - T.AMOUNT_OF_DISCOUNT))/100) END "
					+ "FROM TRN_DAILY T,INT_ERP_AR_RECEIPT I "
					+ "WHERE T.INVOICE_NO = I.BILL_NO "
					+ "AND T.HOSPITAL_CODE = I.HOSPITAL_CODE "
					+ "AND I.DOC_TYPE= 'R' AND I.IS_LAST_RECEIPT ='N' "
					+ "AND I.IS_LOADED = 'N' "
					+ "AND T.IS_PARTIAL = 'N' "
					+ "AND T.LINE_NO = '"+hashData.get("LINE_NO")+"'";
					//System.out.println("UPDATE NONE PARTIAL = " + this.sql);
					try {
						this.dbConn.insert(this.sql);
					} catch (SQLException e) {
						result = false;
						System.out.println("Error Update Partial \"TRN_DAILY\" => " + e.getMessage());
					}
				}
				this.dbConn.commitDB();
			}
			this.sql = "";
			this.sql = "UPDATE INT_ERP_AR_RECEIPT SET "
			+ "IS_LOADED = 'Y' "
			+ "WHERE TRANSACTION_DATE LIKE '"+yyyy+mm+"%' AND IS_LAST_RECEIPT = 'N'";
			try {
				System.out.println("Update : "+this.sql);
				this.dbConn.insert(this.sql);
				this.dbConn.commitDB();
			} catch (SQLException e) {
				System.out.println("Error Update \"INT_ERP_AR_RECEIPT\" => " + e.getMessage());
				result = false;
			}
			this.dbConn.closeDB("");
		return result;
	}	
	private boolean backUpData(String YYYY, String MM){
		//Backup Transaction in TRN_DAILY only match with transaction partial in INT_ERP_AR_RECEIPT
		System.out.println("Start Backup Data for Partial Process : "+JDate.getTime());
		boolean result =true;
		this.dbConn = new DBConn(false);
		this.sql = "UPDATE TRN_DAILY SET "
		+ "AMOUNT_BEF_WRITE_OFF = AMOUNT_AFT_DISCOUNT, "
		+ "DR_AMT_BEF_PARTIAL = DR_AMT, "
		+ "DR_TAX_BEF_PARTIAL = CASE WHEN TAX_TYPE_CODE = '406' THEN DR_TAX_406 ELSE '0' END, "
		+ "OLD_TAX_AMT = CASE WHEN TAX_TYPE_CODE = '406' THEN DR_TAX_406 ELSE '0' END "
		+ "WHERE HOSPITAL_CODE = '"+hospitalCode+"' "
		+ "AND INVOICE_NO IN ("
		+ "SELECT BILL_NO FROM INT_ERP_AR_RECEIPT WHERE HOSPITAL_CODE = '"+hospitalCode+"' "
		+ "AND TRANSACTION_DATE LIKE '"+YYYY+MM+"%' "
		+ "AND DOC_TYPE= 'R' AND IS_LAST_RECEIPT ='N') "
		+ "AND IS_PARTIAL = 'N' ";
		try {
			this.dbConn.setStatement();
			this.dbConn.insert(this.sql);
			this.dbConn.commitDB();
		} catch (SQLException e) {
			this.dbConn.rollDB();
			System.out.println("SQLException in method \"backUpData\" "+e.getMessage());
			result = false;
		}
		this.dbConn.closeDB("");
		System.out.println("Finish Backup Data for Partial Process : "+JDate.getTime());
		return result;
	}
	public boolean rollBack(String YYYY, String MM, String hospitalCode){
		boolean result = true;
		this.dbConn = new DBConn(false);
		String qMessage = "";
		
		String updateTrn = "UPDATE TRN_DAILY SET "
			+ "AMOUNT_AFT_DISCOUNT = AMOUNT_BEF_WRITE_OFF, "
			+ "DR_AMT = DR_AMT_BEF_PARTIAL, "
			+ "DR_TAX_406 = CASE WHEN TAX_TYPE_CODE = '406' THEN DR_TAX_BEF_PARTIAL ELSE '0' END "
			//+ "DR_TAX_406 = DR_TAX_BEF_PARTIAL "
			+ "WHERE HOSPITAL_CODE = '"+hospitalCode+"' "
			+ "AND INVOICE_NO IN ("
			+ "SELECT BILL_NO FROM INT_ERP_AR_RECEIPT WHERE HOSPITAL_CODE = '"+hospitalCode+"' "
			+ "AND TRANSACTION_DATE LIKE '"+YYYY+MM+"%' "
			+ "AND DOC_TYPE= 'R' AND IS_LAST_RECEIPT ='N') "
			+ "AND IS_PARTIAL = 'N' ";
		
		String updateIntErp = "UPDATE INT_ERP_AR_RECEIPT SET "
			+ "IS_LOADED = 'N' "
			+ "FROM INT_ERP_AR_RECEIPT I "
			+ "WHERE HOSPITAL_CODE = '"+hospitalCode+"' AND I.DOC_TYPE= 'R' "
			+ "AND I.IS_LAST_RECEIPT ='N' AND TRANSACTION_DATE LIKE '"+YYYY+MM+"%'";
		
		String deleteTrn = "DELETE TRN_DAILY "
			+ "WHERE HOSPITAL_CODE = '"+hospitalCode+"' AND YYYY = '"+YYYY+"' "
			+ "AND MM = '"+MM+"' AND IS_PARTIAL = 'Y'";
		
		try {
			this.dbConn.setStatement();
			if(this.dbConn.getSingleData("SELECT IS_PARTIAL FROM HOSPITAL WHERE CODE = '"+hospitalCode+"'").equals("Y")){
				System.out.println("Rollback Partial");
				qMessage = updateTrn;
				dbConn.insert(updateTrn);
				qMessage = updateIntErp;
				dbConn.insert(updateIntErp);
				qMessage = deleteTrn;
				dbConn.insert(deleteTrn);
				dbConn.commitDB();				
			}else{
				System.out.println("No Partial Condition");
			}
		} catch (Exception e1) {
			dbConn.rollDB();
			result = false;
			System.out.println("SQLException in stetment : "+qMessage);
		}		
		dbConn.closeDB("Close Connection from Partial Process");
		return result;
	}
}